package com.jugheadzhou.auth.core.message.consumer.receiver;

import com.jugheadzhou.auth.core.common.utils.JsonUtil;
import com.jugheadzhou.auth.core.message.IMessage;
import com.jugheadzhou.auth.core.message.WsEvent;
import com.jugheadzhou.auth.core.message.WsMessage;
import com.jugheadzhou.auth.core.message.producer.sender.RabbitSender;
import com.jugheadzhou.auth.service.UserService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

/**
 * @author jugheadzhou
 * @date 2021-12-02
 */
@Slf4j
public class WsGlobalConReceiver extends AbstractRabbitmqReceiver {

    public static final String QUEUE_NAME = "ws.global.con";

    private UserService userService;


    public WsGlobalConReceiver(UserService userService) {
        this.userService = userService;
    }

    @RabbitListener(queues = QUEUE_NAME, errorHandler = "receiverListenerErrorHandler")
    @RabbitHandler
    @Override
    public void handle(IMessage iMessage, Channel channel, Message message) {
        super.handle(iMessage, channel, message);
    }

    @Override
    public void handleMessage(IMessage iMessage, Channel channel, Message message) throws Exception {
        String dataDtr = iMessage.getData();
        WsMessage data = JsonUtil.string2Object(dataDtr, WsMessage.class);
        if (WsEvent.ON_OPEN.getName().equals(data.getWsEvent())){
            userService.selectInitScoreAndSendMqByWsConnectionId(data.getConnectionId());
        }
    }

}
